//We Read all Scripts with "DV_RLV_" on start and add the next to the Menu List on Click, also we add an Basic Menu Update and such
list gMenu;
list gMenu2;
list gBasicMenu =["<<","END",">>"];
string gMenuPrefix="DV_MAIN_";
integer gMenuLink =1337;
key ToucherID;
integer gDemon =1338;
integer gPlugin =1339;
integer gOwnerAdd =1438;
integer channel_dialog;
integer listen_id;
integer listen_id2;
string msg ="Main Menu\nVersion 0.1 Beta";
string gTakeKey ="Take Key";
string gLeaveKey ="Leave Key";
string gLock ="Lock";
string gUnlock="Unlock";
string gUpdate=" ";
key keyHolder=NULL_KEY;
integer lockState=0;
integer gPage=0;
list ownerList;
integer DV_HS = 1437;
Debug(string _text)
{
    llSay(DEBUG_CHANNEL, _text);
}

list pageMenu(list in ,integer page)
{
	list out;
	integer maxpages =llGetListLength(in)/9;
	page = (page)%(maxpages+1);
	out =["<<","Main",">>"];
	integer _listcount =(page *9)+9;
	integer _i=_listcount-9;
	
	for (_i;_i<_listcount;_i++)
	{
		if(_i<=llGetListLength(in)&&llList2String(in,_i)!="")
		out+=llList2String(in,_i);
	}
	return out;
}

makeMenu()
{
    integer _Scripts = llGetInventoryNumber(INVENTORY_SCRIPT); 
    if (_Scripts == 1)
    {
        llOwnerSay ("No other Scripts inside");
    }
    else if (_Scripts > 1)
    {
        integer i=0;
        gMenu=[];

        while(i<_Scripts)
        {
            string _Name;
            _Name= llGetInventoryName(INVENTORY_SCRIPT, i);
            if(llGetSubString(_Name, 0, llStringLength(gMenuPrefix)-1) ==gMenuPrefix )
            {
            	string strName=llGetSubString(_Name,llStringLength(gMenuPrefix),llStringLength(_Name));
                if(strName!="Update")gMenu +=strName;
                else gUpdate="Update";
            }
            i++; 
        }
    }
}

displayMenu()
{
	list tempMain=[];
	string TTName = llKey2Name(ToucherID); 
	if(keyHolder==ToucherID || ~llListFindList(ownerList, (list)TTName))
    {
    	if(lockState==1)
		{
			tempMain+=[gUnlock];
			tempMain+=gMenu;
		}
		else
		{
			tempMain+=[gLock];
			tempMain+=[gUpdate];
		}
    	tempMain+=[gLeaveKey];
    }
    else if(keyHolder==NULL_KEY)
    {
    	if(lockState==1)
		{
			tempMain+=[gUnlock];
			tempMain+=gMenu;
			
		}
		else
		{
			tempMain+=[gLock];
			tempMain+=[gUpdate];
		}
    	tempMain+=[gTakeKey];
    }
    else if(ToucherID!=keyHolder || ToucherID !=llGetOwner())
    {
    		if(keyHolder!=NULL_KEY)
		   tempMain=[":)"]; 
    }
	
	if(lockState==1)
	{
		msg="is secure locked and \n\n";
	}
	else
	{
		msg="ist unlocked\n\n";
	}
	if(llGetListLength(gMenu)<8)
	{
		tempMain= [" ","End", " "]+tempMain;
	}
	else
	{
		tempMain= pageMenu(tempMain,gPage);
	}
	gMenu2=tempMain;
    channel_dialog = ( 1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
	listen_id = llListen( channel_dialog, "", NULL_KEY, "");
    llDialog(ToucherID, msg,gMenu2, channel_dialog);
    llSetTimerEvent(45.0);
}



default 
{
    state_entry() 
    {
    	ownerList=[];
       makeMenu(); 
       
    }
    
    changed(integer change)
    {
         if (change & CHANGED_INVENTORY)
         { 
             makeMenu();
         }  
    }
    
    touch_start(integer total_number)
    {
        ToucherID = llDetectedKey(0);
        displayMenu();
    }
    
    listen(integer channel, string name, key id, string message)
    {
    	if(channel==3)
    	{
    		ToucherID=id;
  			displayMenu();
    	}
    	if(channel==channel_dialog)
    	{
    		llListenRemove(listen_id);
    		llSetTimerEvent(0.0);
	    	if(message=="<<")
	  		{
	  			gPage--;
				integer maxpages =llGetListLength(gMenu2)/9;
				if(gPage<0)
				{
					gPage = maxpages;
				}
				displayMenu();
	  		}
	  		else if(message==">>")
	  		{
	  			gPage++;
	  			displayMenu();
	  		}
	  		else if (message == "Lock") 
		    {
		    	llSay(0,"Locked by: "+llKey2Name(ToucherID));
		    	llOwnerSay(llKey2Name(ToucherID) + " has  Take you Key's!");
		    	llOwnerSay("@detach=n");
		    	lockState=1;
		    	keyHolder=ToucherID;
		    	displayMenu();
		    	listen_id2 = llListen( 3, "", ToucherID, "menu");
		    	llMessageLinked(LINK_SET, DV_HS, "primShow", ToucherID);
		    	llMessageLinked(LINK_SET, DV_HS, "playAnimation", ToucherID);
		     	
		    }
		    else if(message =="Unlock")
		    {
		    	llSay(0,"Unlocked by: "+llKey2Name(ToucherID));
		    	llOwnerSay("@detach=y");
		    	lockState=0;
		    	displayMenu();
		    	llListenRemove(listen_id2);
		    	llMessageLinked(LINK_SET, DV_HS, "primHide", ToucherID);
		    	llMessageLinked(LINK_SET, DV_HS, "stopAnimation", ToucherID);
		    	
		    }
	  		else if(message =="Take Key")
		    {
		    	if(lockState!=1)
		    	{
		    		llSay(0,llKey2Name(ToucherID)+" taken "+llKey2Name(llGetOwner())+" Key's");
		    		llOwnerSay("@detach=n");
		    		lockState=1;
		    	}
		    	keyHolder=ToucherID;
		    	displayMenu();
		    }
		    else if(message=="Leave Key")
		    {
		    	keyHolder=NULL_KEY;
		    	displayMenu();
		    	llSay(0,llKey2Name(ToucherID)+" has leave " +llKey2Name(llGetOwner()) +"'s Key's");
		    }
		    else if(message=="Update")
		    {
		    	llMessageLinked(LINK_THIS, gMenuLink, gMenuPrefix+message, ToucherID);
		    }
	  		else if(~llListFindList(gMenu, (list)message))
	  		{
	    		llMessageLinked(LINK_THIS, gMenuLink, gMenuPrefix+message, ToucherID);
	  		}
    	}
    }
    
    link_message(integer sender, integer num, string str, key id)
  	{
  		if(num ==gMenuLink)
  		{
  			if(str==llGetScriptName())
  			{
  				ToucherID=id;
  				displayMenu();
  			}
  		}
  		if(num ==gDemon)
  		{
  			if(str!=llGetScriptName())
  			{
				llListenRemove(listen_id);
  			}
  		}
  		if(num==gPlugin)
  		{
  			if(str=="ON_REZ")
  			{
  				
  			}
  			if(str=="RESET")
  			{
  				keyHolder=llGetOwner();
  			}
  		}
  		if(num == gOwnerAdd)
  		{
  			ownerList+=str;
  		}
  	}
    on_rez(integer bla)
    {
    	if(lockState==1)
    	{
    		llOwnerSay("@detach=n");
    	}
    }
    
    timer()
    {
    	llSetTimerEvent(0.0);
    	llListenRemove(listen_id);
    }
    
}
